<template>
  <el-card>
    <div slot="header">
      <h3>新增多供进价-多计量</h3>
    </div>
    <div class="body_wrap">
      <div class="more_supply_info">
        <SupplyInfo
          class="supply_info"
          :supplyInfo="supplyInfo"
          :initSupplyInfo="resetRelativeDataByRechooseSupply"
          @getSupplyRelativeInfo="setSupplyRelativeInfo"
        />
        <BtnRender :render="renderBtnsBarByInfo" />
        <!-- 发起原因输入框 -->
        <LaunchReasonInput :reason.sync="launchReason" />
        <el-divider content-position="center"><i class="el-icon-info"></i>多供信息</el-divider>
        <el-row>
          <SkusTable
            :dataSource="infoTable.list"
            :columns="infoTable.columns"
            :loading="infoTable.loading"
            :defaultMinWidth="100"
            :maxTableHeight="480"
          />
        </el-row>
      </div>
      <div class="more_supply_purchase">
        <el-divider content-position="center"><i class="el-icon-price-tag"></i>多供进价</el-divider>
        <el-row type="flex" align="middle">
          <p>供货价格方式：</p>
          <el-radio-group
            v-model="purchaseType"
            @change="handleSwitchPurchaseStatus"
          >
            <el-radio label="1">统一进价</el-radio>
            <el-radio label="2">区域进价</el-radio>
          </el-radio-group>
        </el-row>
        <el-row type="flex" justify="space-between">
          <el-row type="flex" align="middle">
            <p>设置参照城市关系：</p>
            <el-button type="text" size="large" @click="handleSetReferCityRelation">设置</el-button>
          </el-row>
          <BtnRender :render="renderBtnsBarByPurchase" />
        </el-row>
        <el-row>
          <SkusTable
            :defaultMinWidth="100"
            :maxTableHeight="480"
            :dataSource="PurchaseTable.list"
            :columns="PurchaseTable.columns"
            :loading="PurchaseTable.loading"
            @handleRowChange="handleRowChangeByPurchaseTable"
            @handleCancelSelect="handleRowDeleteByPurchaseTable"
          />
        </el-row>
      </div>
      <!-- 表尾字段 -->
      <FooterFields :footerFields="footerFields" />
    </div>
    <!-- 设置参照城市弹框 -->
    <SetReferenceCity
      :visible.sync="ReferenceCity.visible"
      :supplierCode="supplyInfo.supplierCode"
      :showReferenceCityColumns="ReferenceCity.columns"
      :templateTableData="ReferenceCity.list"
      :getReferenceCityListAction="getCityListByCanSetReferRelation"
      @getSetCityData="getSetCityListByReferRelation"
    />
    <!-- 关联商品-SKU维度弹框 -->
    <GoodsLinkedDialog
      ref="goodsLinked"
      isQueryInit
      :visible.sync="goodsLinked.visible"
      :queryUrl="goodsLinked.queryUrl"
      :extraForms="goodsLinked.extraForms"
      :getSearchParams="getSearchParamsByGoodsLinked"
      @getLinkedRows="getTableDataByGoodsLinked"
    />
    <!-- 高级关联弹框 -->
    <SeniorLinkedDialog
      :visible.sync="seniorLinked.visible"
      :columns="seniorLinked.columns"
      :dataSource="seniorLinked.dataSource"
      :submitLimitLength="seniorLinked.limits"
      @seniorSearchResult="getTableDataBySeniorLinked"
    />
  </el-card>
</template>
<script>
import infoApi from './api';
import infoUtils from './utils';
import { getListByMapDimensionKey } from '@/utils/getUtils';
import { handlePriceChangeForMultipleAdd, hasEmptyPriceForMultiple } from '@/utils/price';
import multipleColumns from './constants/multipleColumns';
import moreSupplyMixins from './mixins';
export default {
  mixins: [moreSupplyMixins],
  methods: {
    // 获取按钮栏预渲染列表集合
    getPrerenderBarListByTableType (type) {
      const infoBars = this.getInfoBars();
      const purchaseBars = this.getPurchaseBars(
        'sysback/supplyareaprice/importTemplateForCreateSteel'
      );
      const listByType = {
        'info': infoBars, // 多供信息
        'purchase': purchaseBars // 多供进价
      }
      return listByType[type] || [];
    },
    // 导出多供进价表格
    exportPurchaseData () {
      let exportFn = this.exportPurchaseDataByUnitType();
      exportFn('2', '新增多供进价模板-多计量');
      exportFn = null;
    },
    // 提交供货信息和价格数据
    submitMoreSupplyData () {
      let submitFn = this.submitMoreSupplyDataByUnitType();
      submitFn(hasEmptyPriceForMultiple, '1');
      submitFn = null;
    },
    // 供货价格表格row变化
    handleRowChangeByPurchaseTable (row) {
      // 表格数据更改需要相关字段进行计算
      handlePriceChangeForMultipleAdd(row);
    },
    // 多供进价表格计算
    countPurchaseTable () {
      this.PurchaseTable.list.forEach(row => {
        // 表格数据更改需要相关字段进行计算
        handlePriceChangeForMultipleAdd(row);
      });
    },
    // 修改页面时，多供进价表格数据初始化
    setInitUnifiedByEdit (supplyAreaPriceParams) {
      let initFn = this.initSupplyAreaPriceByUintType(supplyAreaPriceParams);
      initFn(infoUtils.getInitMultipleTableDataBySupplyPrice);
      initFn = null;
    },
    // 初始供货信息和多供进价表格数据
    initAllTableData (list, setType) {
      this.resetReferCityData();
      this.PurchaseTable.initAll(infoUtils.getInitMultipleTableDataBySupplyPrice(
        list,
        this.taxRateInvoiceInfo,
        this.purchaseType
      ));
      this.infoTable.list = infoUtils.getInitTableDataBySupplyInfo(list);
      this.setColumnsByCanEditLimits({ setType });
    },
    // 获取表格数据 by 高级关联
    async getTableDataBySeniorLinked (skuNos) {
      const retData = await infoApi.getResultsBySeniorSearch({
        selectedSupplyUuid: this.supplyInfo?.supplierCode, // 是高级关联
        isPurchaseMultiUnit: '1', // 2是单计量，1是多计量
        skuNos
      });
      if (retData) {
        this.initAllTableData(retData, 'tableList');
      }
    },
    // 获取关联弹框表格查询入参
    getSearchParamsByGoodsLinked ({ formData, tabState, linkGoods }) {
      const linkFlag = { notLinked: '1', linked: '2' }
      return {
        linkFlag: linkFlag[tabState],
        isPurchaseMultiUnit: '1', // 2是单计量，1是多计量
        selectedSupplyUuid: this.supplyInfo?.supplierCode,
        linkSkuNos: getListByMapDimensionKey(linkGoods, 'skuNo'),
        ...formData
      }
    },
    // 根据是否可编辑状态设置表格表列
    setPurchaseColumnsByEditState (canEdit) {
      let runMethod = this.setPurchaseColumns(multipleColumns);
      runMethod(canEdit);
      runMethod = null;
    }
  }
}
</script>
